﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ThresholdAssistant.DataContract.Models;
using System.IO;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace ThresholdAssistant.Attributes
{
    /// <summary>
    /// 基础控制器
    /// </summary>
    [ApiController]
    public class BaseController : Controller
    {
        /// <summary>
        /// 机器人信息
        /// </summary>
        [JsonProperty("robot_info"), JsonPropertyName("robot_info")]
        public RobotInfoModel RobotInfo { get; set; }

        /// <summary>
        /// 商家信息
        /// </summary>
        [JsonProperty("merchant_info"), JsonPropertyName("merchant_info")]
        public MerchantRobotInfoModel MerchantInfo { get; set; }

        /// <summary>
        /// 机器人授权的好友信息
        /// </summary>
        [JsonProperty("friends_info"), JsonPropertyName("friends_info")]
        public RobotFriendsInfoModel FriendsInfo { get; set; }

        /// <summary>
        /// 机器人授权的群信息
        /// </summary>
        [JsonProperty("chat_room_info"), JsonPropertyName("chat_room_info")]
        public RobotChatRoomInfoModel ChatRoomInfo { get; set; }

        /// <summary>
        /// 获取请求参数的body内容
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public async Task<T> GetRequestBody<T>() where T : class
        {
            var stream = Request.Body;
            using StreamReader streamReader = new StreamReader(stream);
            var postJson = await streamReader.ReadToEndAsync();
            return JsonConvert.DeserializeObject<T>(postJson);
        }
    }
}